from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import users
from models import *

class UserUtil:
  def getUser():
    # Get the logged in user
    loggedInUser = users.get_current_user()
    
    userQuery = User.gql('WHERE uniqueid = :1 LIMIT 1', loggedInUser.user_id())
    # If the user is not in the datastore, add them
    if (userQuery.count() == 0):
      # Add user to db
      user = User()
      user.name = loggedInUser.nickname()
      user.uniqueid = loggedInUser.user_id()
      user.put()
    else:
      # If they are in the db get the user data for returning
      user = userQuery.get()
    
    return user
  createAndReturn = staticmethod(createAndReturn)
